package com.thb.pay.agent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.thb.pay.agent.entity.AgentAccountHistory;
import com.thb.pay.agent.mapper.AgentAccountHistoryMapper;
import com.thb.pay.agent.service.AgentAccountHistoryService;
import com.thb.pay.api.agent.vo.req.AgentAccountHistoryPageReq;
import com.thb.pay.api.agent.vo.req.AgentAccountHistoryReq;
import com.thb.pay.api.agent.vo.resp.AgentAccountHistoryVo;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.db.service.impl.BaseServiceImpl;
import com.thb.pay.db.utils.PageResultUtils;
import com.thb.pay.db.utils.PageUtils;
import com.thb.pay.db.utils.QueryUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 代理商-账户-流水表 服务实现类
 * </p>
 *
 * @author lucky
 * @since 2019-07-21
 */
@Service
public class AgentAccountHistoryServiceImpl extends BaseServiceImpl<AgentAccountHistoryMapper, AgentAccountHistory> implements AgentAccountHistoryService {

    @Override
    public PageResult<AgentAccountHistoryVo> findPage(AgentAccountHistoryPageReq pageReq) {
        IPage<AgentAccountHistory> currentPage = PageUtils.buildPage(pageReq);
        QueryWrapper wrapper = QueryUtils.buildQueryPage(pageReq);
        IPage<AgentAccountHistory> pageResult = super.page(currentPage, wrapper);
        return PageResultUtils.build(pageResult, AgentAccountHistoryVo.class);
    }

    @Override
    public int count(AgentAccountHistoryReq agentAccountHistoryReq) {
        return 0;
    }

    @Override
    public AgentAccountHistoryVo getById(Long id) {
        Assert.notNull(id, "agentAccountHistoryId.not.null");
        AgentAccountHistory agentAccountHistory = super.getById(id);
        if (agentAccountHistory != null) {
            AgentAccountHistoryVo targetResult = AgentAccountHistoryVo.build(AgentAccountHistoryVo.class);
            BeanUtils.copyProperties(agentAccountHistory, targetResult);
        }
        return null;
    }

    @Override
    public AgentAccountHistoryVo getByAgentIdAndId(Long agentId, Long id) {
        Assert.notNull(agentId, "agentId.not.null");
        Assert.notNull(id, "agentAccountHistoryId.not.null");
        LambdaQueryWrapper<AgentAccountHistory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AgentAccountHistory::getId, id);
        wrapper.eq(AgentAccountHistory::getAgentId, agentId);
        AgentAccountHistory agentAccountHistory = super.getOne(wrapper);
        if (agentAccountHistory != null) {
            AgentAccountHistoryVo targetResult = AgentAccountHistoryVo.build(AgentAccountHistoryVo.class);
            BeanUtils.copyProperties(agentAccountHistory, targetResult);
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> countAgentProfit(Long agentId) {
        Assert.notNull(agentId, "agentId.not.null");
        QueryWrapper<AgentAccountHistory> wrapper = new QueryWrapper<>();
        wrapper.select("COUNT(1) AS totalCount,SUM(amount) AS totalProfit,biz_item As bizItem");
        wrapper.eq("agent_id", agentId);
        wrapper.eq("biz_type", 1);
        List<Map<String, Object>> resResultList = super.selectMaps(wrapper);
        return resResultList;
    }
}
